<?php
/**
 * @version		$Id: plugins.php 138 2011-04-04 18:30:34Z happy_noodle_boy $
 * @package		JCE
 * @copyright	Copyright (C) 2009 Ryan Demmer. All rights reserved.
 * @copyright	Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
 * @license		GNU/GPL
 * This version may have been modified pursuant
 * to the GNU General Public License, and as distributed it includes or
 * is derivative of works licensed under the GNU General Public License or
 * other free or open source software licenses.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

// load base model
require_once(dirname(__FILE__) . DS . 'wf.php');

class WFModelPlugins extends WFModel {
	 
	function getCommands()
	{
		$xml  = & JFactory::getXMLParser('Simple');
		$file = dirname(__FILE__).DS.'commands.xml';

		$commands = array();

		if (is_file($file) && $xml->loadFile($file)) {
			$elements = WFXMLHelper::getElements($xml, 'commands');

			foreach ($elements->children() as $element) {
				$name = $element->attributes('name');

				if ($name) {
					$commands[$name] = new StdClass();

					foreach ($element->children() as $item) {
						$key   = $item->name();
						$value = $item->data();
						 
						$commands[$name]->$key = $value;
					}
					
					$commands[$name]->type = 'command';
				}
			}
		}

		return $commands;
	}

	function getPlugins() {
		jimport('joomla.filesystem.folder');

		$plugins = array();

		// get core xml
		$core = WFXMLHelper::loadFile(dirname(__FILE__) . DS . 'plugins.xml');
		if ($core) {
			$elements 	= WFXMLHelper::getElements($core, 'plugins');		
			foreach ($elements->children() as $element) {
				$name = $element->attributes('name');
	
				if ($name) {
					$plugins[$name] = new StdClass();
	
					foreach ($element->children() as $item) {
						$key   = $item->name();
						$value = $item->data();
						 
						$plugins[$name]->$key = $value;
					}
					
					$plugins[$name]->type = 'plugin';
					
					$plugins[$name]->author       = '';
					$plugins[$name]->version      = '';
					$plugins[$name]->creationdate = '';
				}
			}
		}

		// get all Plugins
		$folders = JFolder::folders(WF_EDITOR_PLUGINS, '.', false, true, array_merge(array('.svn', 'CVS'), array_keys($plugins)));
		
		foreach ($folders as $folder) {
			$name 	= basename($folder);			
			$xml 	= WFXMLHelper::loadFile($folder.DS.$name.'.xml');
			
			if ($xml) {
				$plugins[$name] = new StdClass();

				$plugins[$name]->name         = $name;

				$plugins[$name]->title        = WFXMLHelper::getElement($xml, 'name');
				$plugins[$name]->icon         = WFXMLHelper::getElement($xml, 'icon');

				$plugins[$name]->author       = WFXMLHelper::getElement($xml, 'author');
				$plugins[$name]->version      = WFXMLHelper::getElement($xml, 'version');
				$plugins[$name]->creationdate = WFXMLHelper::getElement($xml, 'creationdate');

				$params = WFXMLHelper::getElements($xml, 'params');

				$editable = intval(WFXMLHelper::getAttribute($xml, 'editable'));
				$plugins[$name]->editable = $editable ? $editable : ($params && count($params) ? 1 : 0);

				$plugins[$name]->row = WFXMLHelper::getAttribute($xml, 'row', 4);

				$core = WFXMLHelper::getAttribute($xml, 'core');

				$plugins[$name]->core = $core ? 1 : 0;	
				$plugins[$name]->type = 'plugin';
			}
		}

		return $plugins;

	}
	
	/**
     * Get a plugin's extensions
     * @param object $plugin
     * @return 
     */
    function getExtensions()
    {
        jimport('joomla.filesystem.folder');
        jimport('joomla.filesystem.file');
        
        $extensions = array();
        
        // Load xml file
        $xml =& JFactory::getXMLParser('Simple');
		// recursively get all extension files
        $files = JFolder::files(WF_EDITOR_EXTENSIONS, '\.xml$', true, true);
                
                foreach ($files as $file) {
                    $object           		= new StdClass();
                    $object->folder   		= basename(dirname($file));
                    $object->manifest 		= $file;
                    $object->plugins  		= array();
                    $name             		= basename($file, '.xml');
                    $object->name     		= $name;
                    $object->description 	= '';
                    
                    $xml =& JFactory::getXMLParser('Simple');

                    if ($xml->loadFile($file)) {
                        $plugins = WFXMLHelper::getElement($xml, 'plugins');
                        if ($plugins) {
                        	$object->plugins = is_string($plugins)? explode(',', $plugins) : $plugins;
                        }

                        $object->name 			= WFXMLHelper::getElement($xml, 'name');
                        $object->description 	= WFXMLHelper::getElement($xml, 'description');
                        $object->folder			= WFXMLHelper::getAttribute($xml, 'folder');
                        $core					= WFXMLHelper::getAttribute($xml, 'core');
                        $object->core 			= $core ? 1 : 0;
                        
                        // load language
	                    $language =& JFactory::getLanguage();
	                    $language->load('com_jce_' . $object->folder . '_' . $name, JPATH_SITE);
                    }
                    $object->extension = $name;
                    $extensions[]      = $object;
                }
        
        return $extensions;
    }
	
	/**
	 * Process import data from XML file
	 * @param object $file XML file
	 * @param boolean $install Can be used by the package installer
	 * @return
	 */
	function processImport($file, $install = false)
	{
		return true;
	}
}